package milktea.servlet.product;

import milktea.entity.CATEGORY;
import milktea.entity.PRODUCT;
import milktea.service.CATEGORYService;
import milktea.service.PRODUCTService;

import java.io.IOException;
import java.util.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Servlet implementation class ToProductUpdate
 */
@WebServlet("/manage/admin_toproductupdate")
public class ToProductUpdate extends HttpServlet {
    private static final long serialVersionUID = 1L;

    /**
     * @see HttpServlet#HttpServlet()
     */
    public ToProductUpdate() {
        super();
        // TODO Auto-generated constructor stub
    }

    /**
     * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//        request.setCharacterEncoding("UTF-8");
//        response.setContentType("text/html;charset=utf-8");
        // TODO Auto-generated method stub
//        response.getWriter().append("Served at: ").append(request.getContextPath());
        int id = Integer.parseInt(request.getParameter("id"));
        PRODUCT product = PRODUCTService.selectById(id);
//        ArrayList<CATEGORY> clist = CATEGORYService.selectAll();
//
//        ArrayList<CATEGORY>flist=new ArrayList<>(clist);
//
//        for (CATEGORY c : clist) {
//            if(c.getCATE_PARENT_ID()!=0){
//                flist.remove(c);
//            }
//        }

//        request.setAttribute("clist",clist);
//        request.setAttribute("flist",flist);
        ArrayList<CATEGORY> flist = CATEGORYService.selectCat("father");
        request.setAttribute("flist", flist);

        ArrayList<CATEGORY> clist = CATEGORYService.selectCat("child");
        request.setAttribute("clist", clist);
        request.getSession().setAttribute("product", product);
        request.getRequestDispatcher("/manage/admin_productmodify.jsp").forward(request, response);
    }

    /**
     * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
     */
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        // TODO Auto-generated method stub
        doGet(request, response);
    }

}
